$(function()
{
	// suppression d'un post (annonce ou news) et de ses réponses
	$('a.js-del-post').click(function() {
		var id = $(this).attr('id').split('_')[1];
		var self = this;
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=post_delete'
				+ '&id=' + id,
			
			success: function(result) {
				result = result.split('&&&')[0];
				if (result == 'ok') {
					$(self).closest('tr').animate({
						'backgroundColor': '#ff8888',
						'color': '#941010'
					}, 500);
					$(self).closest('tr').fadeOut();
				}
				else {
					alert('Erreur lors de la suppression du sujet');
				}
			}
		});
	});

	// affichage des infos d'un contenu
	$(function() {
		$('div#details').dialog({
			autoOpen: false,
			modal: true,
			closeOnEscape: true,
			resizable: true,
			width:500
		});
	});

	$('a.js-ask-details').click(function() {
		var id = $(this).attr('id').split('-')[1];
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=content_details'
				+ '&id=' + id,
			
			success: function(result) {
				result = result.split('&&&');
				if(result[0] == 'ok') {
					$('div#details').html(result[1]);
					$('div#details').dialog('open');
				}
				else alert('Erreur lors de la récupération des détails du contenu');
			}
		});
	});
	
	
	// suppression d'un contenu
	$('a.js-del-content').click(function() {
		var id = $(this).attr('id').split('-')[1];
		var self = this;
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=content_delete'
				+ '&id=' + id,
			
			success: function(result) {
				result = result.split('&&&')[0];
				if (result == 'ok') {
					$(self).closest('tr').animate({
						'backgroundColor': '#ff8888',
						'color': '#941010'
					}, 500);
					$(self).closest('tr').fadeOut();
				}
				else {
					alert('Erreur lors de la suppression du contenu');
				}
			}
		});
	});

	
	// ajout et modification d'un contenu
	$('input#content_add, input#content_update').click(function() {
		$('div.ajax').html('<img src="images/ajax-loader.gif" alt="Enregistrement..." />');
		
		tinyMCE.triggerSave();
		
		var action = $('input#action').val();
		var old_menu = $('input#old_menu').val();
		var idmenu = $('select#menu').val();
		var old_id = $('input#old_id').val();
		var id = $('input#string_id').val();
		var title = $('input#title').val();
		var content = $('textarea#content').val(); content = escape(content);
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'POST',
			data: 'action=' + action
				+ '&old_menu=' + old_menu
				+ '&idmenu=' + idmenu
				+ '&old_id=' + old_id
				+ '&id=' + id
				+ '&title=' + title
				+ '&content=' + content,
			
			success: function(result) {
				result = result.split('&&&');
				if (result[0] == 'ok' || result[0] == 'errors') {
					if(result[0] == 'ok' && action == 'content_add' && result[1] != -1)
						$('option[value=' + result[1] + ']').remove();
					$('div.ajax').html(result[2]);
				}
				else {
					$('div.ajax').html('<p class="error">Erreur lors du traitement du contenu</p>');
				}
			}
		});
		
		return false;
	});
	
	
	// envoi d'un mail groupé
	$('input[name=groupmail_send]').click(function() {
		$('div.ajax').html('<img src="images/ajax-loader.gif" alt="En cours..." /><p>Envoi en cours, cela peut être long...</p>');
		
		tinyMCE.triggerSave();
		
		var content = $('textarea#content').val(),
			subject = $('input#subject').val(),
			status = '&';
		content = escape(content);
		
		if(!$('input[type=checkbox]').is(':checked'))
			status += 'status[]=0&';
		else {
			$('input[type=checkbox]:checked').each(function() {
				status += 'status[]=' + $(this).val() + '&';
			});
		}
		
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'POST',
			data: 'action=mailgroup_send'
				+  status // Contient les '&' avant ET après
				+ 'content=' + content
				+ '&subject=' + subject,
			
			success: function(result) {
				result = result.split('&&&');
				if (result[0] == 'ok') {
					$('div.ajax').html(result[1]);
				}
				else {
					$('div.ajax').html('<p class="error">Erreur lors de l\'envoi du mail</p>');
				}
			}
		});
		
		return false;
	});
	
	var myself = $('input[type=checkbox]#id_1'),
		checkboxes = $('input[type=checkbox]');
	myself.click(function() {
		if($(this).is(':checked')) {
			checkboxes.each(function() {
				if($(this).attr('id') != myself.attr('id')) $(this).attr('checked', false).attr('disabled', true);
			});
		}
		else
			checkboxes.attr('disabled', false);
	});
});

